package com.gudaomai.ekproject.web;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.gudaomai.ekproject.common.CommonConstant;
import com.gudaomai.ekproject.common.CommonException;
import com.gudaomai.ekproject.common.MsgConstant;
import com.gudaomai.ekproject.entity.RetEntity;
import com.gudaomai.ekproject.entity.Role;
import com.gudaomai.ekproject.entity.Sysuser;
import com.gudaomai.ekproject.entity.SysuserInput;
import com.gudaomai.ekproject.entity.UserRole;
import com.gudaomai.ekproject.service.IRoleService;
import com.gudaomai.ekproject.service.ISysuserService;
import com.gudaomai.ekproject.service.IUserRoleService;
import com.gudaomai.ekproject.web.BaseController;

/**
 * <p>
 * sysuser 前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-06-26
 */
@RestController
@RequestMapping("/ekproject/sysuser")
public class SysuserController extends BaseController {

	@Autowired
	ISysuserService sysuerServ;
	@Autowired
	IRoleService roleServ;
	@Autowired
	protected MessageSource msgSource;
	@Autowired
	IUserRoleService userRoleServ;
	@Autowired
	private PasswordEncoder passwordEncoder;

	/*
	 * TODO:complete 新加用户 1. 非空检查 2. 密码加密 3. 缺省用户角色赋值
	 */
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public RetEntity add(@RequestBody @Validated SysuserInput userInput) throws CommonException {

		RetEntity entity = new RetEntity();

		// 如果查询数据库发现已经存在，则拒绝执行if{}后的代码
		if (sysuerServ.isExist(userInput.getName())) {
			// CommonConstant.RESULT_CODE_CREATED @see
			// CommonConstant,MsgConstant
			// RESULT_CODE_CREATED 201 表明用户已经存在
			throw new CommonException(CommonConstant.RESULT_CODE_CREATED,
					getMessage(MsgConstant.RET_USER_ADD_ALREADY_EXIST));
		} else {
			Sysuser user = new Sysuser();
			BeanUtils.copyProperties(userInput, user);/*-?|SysuserController|zenghualu|c0|*/
			// 查询数据库未存在，进行密码加密和缺省用户角色赋值
			String password = user.getPassword();

			String passwordEncty = passwordEncoder.encode(password);
			user.setPassword(passwordEncty);
			// 为用户添加用户id
			user.setId(IdWorker.get32UUID());

			// 给用户增加初始角色<--在user_role表中插入一条新记录
			UserRole userRole = new UserRole();
			// 设置user_role表的主键
			userRole.setId(IdWorker.get32UUID());
			// 该条记录存放用户id
			userRole.setUid(user.getId());

			// 该条记录存放角色id
			// 从role表中查找用户初始的身份值
			Role initRole = roleServ.getInitRole();
			userRole.setRoleId(initRole.getId());

			// 更新用户表和用户角色表
			userRoleServ.insert(userRole);
			sysuerServ.insert(user);/*-|SysuserController|zenghualu|c0|?*/

			// 返回成功插入状态信息
			entity.setRetCode(CommonConstant.RESULT_CODE_OK);
			entity.setRetMessage(getMessage(MsgConstant.RET_USER_ADD_SUCCESS));
		}

		return entity;
	}

	/*
	 * 根据ID查找用户 password字段清空，不返回前端 @see line 141
	 */
	@GetMapping(value = "/id/{id}")
	public Sysuser getUserById(@PathVariable("id") String id) throws Exception {

		Sysuser sysuser = sysuerServ.selectById(id);

		if (sysuser != null) {
			sysuser.setRetCode(CommonConstant.RESULT_CODE_OK);
			sysuser.setRetMessage(MsgConstant.RET_USER_SELECT_ALREADY_EXIST);
			sysuser.setPassword("");

		} else {
			throw new CommonException(CommonConstant.RESULT_CODE_NOT_FOUND, MsgConstant.RET_USER_SELECT_NOTEXIST);
		}

		return sysuser;
	}

}
